﻿using System;
using System.Collections.Generic;
namespace Core
{
    public class TimerComponent : Entity, IUpdate
    {
        private List<Timer> timers = new List<Timer>();
        private Stack<int> removes = new Stack<int>();
        public Timer CreateTimer(float timer)
        {
            var t = new Timer(timer);
            timers.Add(t);
            return t;
        }
        public Timer CreateTimer(float timer, Action action)
        {
            var t = new Timer(timer, action);
            timers.Add(t);
            return t;
        }

        void IUpdate.Update()
        {
            for (int i = 0; i < timers.Count; i++)
            {
                (timers[i] as IUpdate).Update();
                if (!timers[i].isRuning)
                {
                    removes.Push(i);
                }
            }

            while (removes.Count > 0)
            {
                timers.RemoveAt(removes.Pop());
            }
        }
    }
}